﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.DirectoryServices;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Input;
using System.Threading;
namespace WinUserAcc
{
    public partial class frmLogin : Form
    {
        public frmLogin()
        {
            InitializeComponent();
        }

     
        private void btnLogin_Click(object sender, EventArgs e)
        {

            Common.Domain = txtDomain.Text;

            Cursor = Cursors.WaitCursor; //获取等待光标
            var userName = txtUserName.Text;
            var pwd = txtPwd.Text;
            if (CheckLogin(userName, pwd))
            {
                if (cmbType.SelectedIndex ==0)
                {
                    var frmu = new frmUserAcc();
                    frmu.login = this;
                    frmu.UserName = userName; //传值（输入登录用户名）
                    frmu.Pwd = pwd;
                    frmu.Show(); //显示批量创建域用户窗口
                    Hide(); //隐藏登录窗口

                }
                else
                {
                    var frm = new frmComputercs();
                    frm.login = this;
                    frm.UserName = userName;
                    frm.Pwd = pwd;
                    frm.Show(); //显示批量创建计算机窗口
                    Hide();//隐藏登录窗口
                
                }
                Cursor = Cursors.Default; 

                Properties.Settings.Default.username = userName; //在程序运行输入的用户名赋值给配置变量
                Properties.Settings.Default.domain = txtDomain.Text;
                Properties.Settings.Default.Save();//来保存登录用户信息
                Cursor = Cursors.Arrow; //获取等待光标

               
            }
            else
            {
                MessageBox.Show("用户名和密码输入有误，请确认后登录！", "提示", MessageBoxButtons.OK, MessageBoxIcon.None);
            }
         
            
        }

   
        #region 验证非空
        /// <summary>
        /// 验证非空
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        private bool CheckLogin(string userName, string pwd)
        {
            if (Common.GetSearchResult(Common.Domain, userName, pwd) == null)
            {
                return false;
            }

            return true;


        }

        #endregion

        #region  窗体首次加载事件
        private void frmLogin_Load(object sender, EventArgs e)
        {
            //如果配置变量值不为空
            if (!string.IsNullOrEmpty(Properties.Settings.Default.username))
            {
                txtUserName.Text = Properties.Settings.Default.username;//将配置变量的值赋值给文本
                txtDomain.Text = Properties.Settings.Default.domain;
                this.cmbType.SelectedIndex = 0;
            }
        }

        #endregion
      
    }
}
